//
//  WMCustomButton.m
//  FiveHealth
//
//  Created by Mr.Wang on 2018/8/28.
//  Copyright © 2018年 com.wukangcheng.fivehealths. All rights reserved.
//

#import "WMCustomButton.h"

@interface WMCustomButton()

@property (nonatomic, strong) UIImageView *btnImageView;

@property (nonatomic, strong) UILabel *btnTitleLabel;

@end


@implementation WMCustomButton

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        
        self.backgroundColor = [UIColor whiteColor];
        [self layoutSubControls];
        [self configClickAction];
    }
    return self;
}

- (void) configClickAction {
    [self addTarget:self action:@selector(buttonDidClick:) forControlEvents:UIControlEventTouchUpInside];
}
- (void) buttonDidClick:(UIButton *)button {
    if ([self.delegate respondsToSelector:@selector(wmCustomButton:didClickWithTag:)]) {
        [self.delegate wmCustomButton:self didClickWithTag:self.tag];
    }
}

- (void) layoutSubControls {
    
    UILabel *label = [UILabel new];
    label.frame = CGRectZero;
    label.textColor = kDLNormalBlackColor;
    label.numberOfLines = 0;
//    label.lineBreakMode = NSLineBreakByWordWrapping;
    label.textAlignment = NSTextAlignmentCenter;
    [self addSubview:label];
    self.btnTitleLabel = label;
    
    UIImageView *imageView = [UIImageView new];
    imageView.frame = CGRectZero;
    imageView.contentMode = UIViewContentModeScaleAspectFit;
    [self addSubview:imageView];
    self.btnImageView = imageView;
//    imageView.backgroundColor =  kDLBtnNormalBgColor;
    imageView.backgroundColor =  [UIColor whiteColor];
    
}

- (void) updateSubControls {
    
    
//    NSLog(@"================%.2f", self.frame.size.width / self.frame.size.height);
    
    double WHScale = self.frame.size.width / self.frame.size.height;
    
    if (WHScale <= 0.75) {
        //垂直矩形
        self.shapeType = WMCustomButtonTypeVerticalRectangle;
    } else if (WHScale >= 1.33) {
        //水平矩形
        self.shapeType = WMCustomButtonTypeHorizontalRectangle;
    } else {
        //正方形
        self.shapeType = WMCustomButtonTypeQuare;
    }
    
    
    
    switch (self.contentMode) {
        case WMCustomButtonContentModeImageTop:
        {
            
            if (self.shapeType == WMCustomButtonTypeVerticalRectangle) {
                //垂直矩形 图片在上
                self.btnImageView.frame = CGRectMake(self.width *0.05, self.width *0.05, self.width *0.9, self.width *0.9);
                
                self.btnTitleLabel.frame = CGRectMake(0, self.btnImageView.bottom, self.width, self.height - self.btnImageView.bottom);
            } else {
                //正方形
                self.btnImageView.frame = CGRectMake(self.frame.size.width *0.3, self.frame.size.height *0.1, self.frame.size.width *0.4, self.frame.size.height *0.4);
                
                self.btnTitleLabel.frame = CGRectMake(self.frame.size.width * 0.05, CGRectGetMaxY(self.btnImageView.frame) + self.frame.size.height * 0.1, self.frame.size.width *0.9, self.frame.size.height *0.3);
            }
            
        }
            break;
        case WMCustomButtonContentModeImageLeft:
        {
            
            if (self.shapeType == WMCustomButtonTypeHorizontalRectangle) {
                //水平矩形 图片在左
                self.btnImageView.frame = CGRectMake(self.height *0.05, self.height *0.05, self.height *0.9, self.height *0.9);
                
                self.btnTitleLabel.frame = CGRectMake(self.btnImageView.right, 0, self.width - self.height + self.height *0.05, self.height);
            } else {
                //正方形
                self.btnImageView.frame = CGRectMake(self.frame.size.width *0.1, self.frame.size.height *0.3, self.frame.size.width *0.4, self.frame.size.height *0.4);
                
                self.btnTitleLabel.frame = CGRectMake(CGRectGetMaxX(self.btnImageView.frame) + self.frame.size.width *0.1, CGRectGetMinY(self.btnImageView.frame), self.frame.size.width *0.3, self.frame.size.height *0.4);
            }
        }
            break;
        case WMCustomButtonContentModeImageBottom:
        {
            if (self.shapeType == WMCustomButtonTypeVerticalRectangle) {
                //垂直矩形 图片在下
                
                self.btnTitleLabel.frame = CGRectMake(0, 0, self.width, self.height - self.width + self.width *0.05);
                
                self.btnImageView.frame = CGRectMake(self.width *0.05, self.btnTitleLabel.bottom, self.width *0.9, self.width *0.9);
            } else {
                //正方形
                self.btnImageView.frame = CGRectMake(self.frame.size.width *0.3, self.frame.size.height *0.5, self.frame.size.width *0.4, self.frame.size.height *0.4);
                
                self.btnTitleLabel.frame = CGRectMake(self.frame.size.width *0.1, CGRectGetMinY(self.btnImageView.frame) - self.frame.size.height * 0.4, self.frame.size.width *0.8, self.frame.size.height *0.3);
            }
        }
            break;
        case WMCustomButtonContentModeImageRight:
        {
            if (self.shapeType == WMCustomButtonTypeHorizontalRectangle) {
                //水平矩形 图片在右
                self.btnTitleLabel.frame = CGRectMake(0, 0, self.width - self.height + self.height *0.05, self.height);
                
                self.btnImageView.frame = CGRectMake(self.btnTitleLabel.right, self.height *0.05, self.height *0.9, self.height *0.9);
                
                
            } else {
                //正方形
                self.btnImageView.frame = CGRectMake(self.frame.size.width *0.5, self.frame.size.height *0.3, self.frame.size.width *0.4, self.frame.size.height *0.4);
                
                self.btnTitleLabel.frame = CGRectMake(CGRectGetMinX(self.btnImageView.frame) - self.frame.size.width *0.4, CGRectGetMinY(self.btnImageView.frame), self.frame.size.width *0.3, self.frame.size.height *0.4);
            }
        }
            break;
        default:
            break;
    }
}

- (void) setWmBtnImageName:(NSString *)wmBtnImageName {
    _wmBtnImageName = wmBtnImageName;
    NSString *wmImageName = [NSString stringWithFormat:@"%@", wmBtnImageName];
    if ([wmImageName hasPrefix:@"http"]) {
        [self.btnImageView sd_setImageWithURL:[NSURL URLWithString:wmImageName] ];
    } else {
        self.btnImageView.image = [UIImage imageNamed:wmImageName];
    }
}

- (void) setWmBtnTitle:(NSString *)wmBtnTitle {
    _wmBtnTitle = wmBtnTitle;
    NSString *wmTitle = [NSString stringWithFormat:@"%@", wmBtnTitle];
    self.btnTitleLabel.text = wmTitle;
}

- (void) setWmBtnTitleFont:(UIFont *)wmBtnTitleFont {
    _wmBtnTitleFont = wmBtnTitleFont;
    self.btnTitleLabel.font = wmBtnTitleFont;
}

- (void) setWmBtnTitleTextColor:(UIColor *)wmBtnTitleTextColor {
    _wmBtnTitleTextColor = wmBtnTitleTextColor;
    self.btnTitleLabel.textColor = wmBtnTitleTextColor;
}

- (void) setWmBtnTitleLabelBackgroundColor:(UIColor *)wmBtnTitleLabelBackgroundColor {
    _wmBtnTitleLabelBackgroundColor = wmBtnTitleLabelBackgroundColor;
    self.btnTitleLabel.backgroundColor = wmBtnTitleLabelBackgroundColor;
}



/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
